package LibDL.data;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class _NumUtils {

    static IntStream streamAdd(IntStream intStream, int value) {
        IntStream out = intStream.map(x -> x + value);
        intStream.close();
        return out;
    }
    static int[] streamAdd_s(int[] self, int value) {
        return IntStream.of(self).map(x -> x + value).toArray();
    }

    static int[] streamAdd_p(int[] self, int value) {
        return IntStream.of(self).parallel().map(x -> x + value).toArray();
    }

    public static Object[] streamAdd4(Integer[] self, int value) {
        return  Stream.of(self).map(x -> x + value).toArray();
    }

    static int[] streamAdd1(IntStream intStream, int value) {
        return intStream.parallel().map(x -> x + value).toArray();
    }

    static void arrAdd(Integer[] self, int len, int value) {
        for (int i = 0; i < len; i++) {
            self[i] = self[i] + value;
        }
    }
    static void arrAdd2(int[] self, int len, int value) {
        for (int i = 0; i < len; i++) {
            self[i] = self[i] + value;
        }
    }

    static List<Integer> listAdd_stream(List<Integer> self, int value) {
        return self.parallelStream().map(x -> x + value).collect(Collectors.toList());
    }

    static void arrAdd1(Integer[] self, int len, int value, int threadNum) {
        int threadSize = len/threadNum;
        ExecutorService pool = Executors.newFixedThreadPool(8);
        int begin;
        int end;
        for(int i=0;i<threadNum;i++){
            if(i == threadNum - 1){
                begin=threadSize * i;
                end=len;
            } else {
                begin=threadSize * i;
                end=threadSize * (i + 1);
            }
            _NumUtils.MyThread task = new _NumUtils.MyThread(self,begin,end,value);
            pool.execute(task);
        }
        pool.shutdown();
        while(true){
            if(pool.isTerminated()) {
                break;
            }
        }
    }

    static class MyThread extends Thread{
        private Integer[] self;
        private int begin;
        private  int end;
        private int value;
        MyThread(Integer[] self, int begin, int end, int value) {
            this.self = self;
            this.begin=begin;
            this.end=end;
            this.value=value;
        }
        public void run() {
            for(int i=begin;i<end;i++){
                self[i] = self[i] + value;
            }
        }
    }
}
